package com.villaday.idf.custom.pop;

import android.content.Context;
import android.graphics.Color;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.adapter3.ShopOrderBottomAdapter;
import com.villaday.idf.adapter3.ShopOrderTopAdapter;
import com.villaday.idf.model3.DataEntity;
import com.villaday.idf.model3.HashOrdersEntity;
import com.villaday.idf.util.PopularUtil;

import java.util.ArrayList;
import java.util.List;

import razerdp.basepopup.BasePopupWindow;

public class ShopOrderDetailsPop extends BasePopupWindow implements View.OnClickListener {

    private Context mContext;
    private ShopOrderTopAdapter mShopOrderTopAdapter;
    private ShopOrderBottomAdapter mShopOrderBottomAdapter;
    private String[] mTitleTop = {"订单编号", "别墅名称", "房源编号", "房客", "住离日期", "金额"};
    private final HashOrdersEntity.BodyBean.ListBean mListBean;


    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popular_shop_order_details);
    }


    public ShopOrderDetailsPop(Context context, String data) {
        super(context);
        mContext = context;

        setBackground(0);
        setOutSideDismiss(true);
        setKeepSize(true);
        setPopupFadeEnable(false);
        setBackgroundColor(Color.parseColor("#B3000000"));
        setBlurBackgroundEnable(true);
//        findViewById(R.id.ll_sod).startAnimation(PopularUtil.buildAnimal());

        mListBean = new Gson().fromJson(data, HashOrdersEntity.BodyBean.ListBean.class);

        ImageView ivClose = findViewById(R.id.iv_sod_close);
        TextView tvCancel = findViewById(R.id.tv_sod_cancel);
        TextView tvConfirm = findViewById(R.id.tv_sod_confirm);
        ivClose.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        initAdapterTop();
        initAdapterBottom();

        List<DataEntity> list = new ArrayList<>();
        for (int i = 0; i < mTitleTop.length; i++) {
            DataEntity dataEntity = new DataEntity();
            dataEntity.setTitle(mTitleTop[i]);
            if (i == 0) {
                dataEntity.setContent(mListBean.getOrderNo());
            } else if (i == 1) {
                dataEntity.setContent(mListBean.getProduct().getName());
            } else if (i == 2) {
                dataEntity.setContent(mListBean.getProduct().getNumber());
            } else if (i == 3) {
                dataEntity.setContent(mListBean.getCustomer().getName());
            } else if (i == 4) {
                dataEntity.setContent(mListBean.getCheckInDateTime() + "-" + mListBean.getCheckOutDateTime());
            } else {
                dataEntity.setContent(mListBean.getCostPrice());
            }
            list.add(dataEntity);
        }
        mShopOrderTopAdapter.setData(list);

        List<HashOrdersEntity.BodyBean.ListBean.StepsBean> steps = mListBean.getSteps();
        mShopOrderBottomAdapter.setData(steps);

        if (mListBean.getState().getText().equals("待确认")|| mListBean.getState().getText().equals("确认中")) {
            findViewById(R.id.vw_sod_bottom).setVisibility(View.VISIBLE);
            findViewById(R.id.ll_sod_bottom).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.vw_sod_bottom).setVisibility(View.GONE);
            findViewById(R.id.ll_sod_bottom).setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_sod_close:
                dismiss();
                break;
            case R.id.tv_sod_cancel:
                onBtnClickListener.onBtnClick(v, "拒单", mListBean.getOrderNo());
                dismiss();
                break;
            case R.id.tv_sod_confirm:
                onBtnClickListener.onBtnClick(v, "确认", mListBean.getOrderNo());
                dismiss();
                break;


        }
    }

    OnBtnClickListener onBtnClickListener;

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onBtnClick(View view, String title, String content);
    }


    /**
     * 适配底部Recycle
     */
    private void initAdapterBottom() {
        RecyclerView rvSodTop = findViewById(R.id.rv_sod_top);
        rvSodTop.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mShopOrderTopAdapter = new ShopOrderTopAdapter(mContext);
        rvSodTop.setAdapter(mShopOrderTopAdapter);
    }


    /**
     * 适配顶部Recycle
     */
    private void initAdapterTop() {
        RecyclerView rvSodBottom = findViewById(R.id.rv_sod_bottom);
        rvSodBottom.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mShopOrderBottomAdapter = new ShopOrderBottomAdapter(mContext);
        rvSodBottom.setAdapter(mShopOrderBottomAdapter);
    }


    @Override
    protected Animation onCreateShowAnimation() {
        return PopularUtil.ShowAnimation();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return PopularUtil.dismissAnimation();
    }


}
